<template>
    <div class="views-shangpinxinxi-list">
        <div>
            <el-card class="box-card">
                <template #header>
                    <div class="clearfix">
                        <span class="title"> 商品信息查询 </span>
                    </div>
                </template>

                <div class="form-search">
                    <el-form @submit.prevent.stop :inline="true" size="small">
                        <el-form-item label="商品名称">
                            <el-input v-model="search.shangpinmingcheng"></el-input>
                        </el-form-item>
                        <el-form-item label="商品分类">
                            <el-select v-model="search.shangpinfenlei"
                                ><el-option label="请选择" value=""></el-option
                                ><e-select-option type="option" module="shangpinfenlei" value="id" label="fenleiming"></e-select-option
                            ></el-select>
                        </el-form-item>
                        <el-form-item label="价格">
                            <el-input type="text" style="width: 80px" v-model="search.jiage_start"></el-input>
                            -<el-input type="text" style="width: 80px" v-model="search.jiage_end"></el-input>
                        </el-form-item>
                        <el-form-item label="状态">
                            <el-input v-model="search.zhuangtai"></el-input>
                        </el-form-item>
                        <el-form-item label="发布时间">
                            <div style="display: flex">
                                <el-date-picker v-model="search.addtime_start" type="date" value-format="YYYY-MM-DD" placeholder="选择开始日期" />
                                -
                                <el-date-picker v-model="search.addtime_end" type="date" value-format="YYYY-MM-DD" placeholder="选择结束日期" />
                            </div>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" @click="searchSubmit" icon="el-icon-search">查询</el-button>
                        </el-form-item>
                    </el-form>
                </div>

                <el-table border :data="lists" style="width: 100%" @sort-change="sortChange" highlight-current-row>
                    <el-table-column type="index" label="#"></el-table-column>
                    <!-- 序号 -->

                    <el-table-column prop="shangpinmingcheng" label="商品名称">
                        <template #default="{row}"> {{ row.shangpinmingcheng }} </template>
                    </el-table-column>
                    <el-table-column prop="shangpinfenlei" label="商品分类" width="80">
                        <template #default="{row}"> <e-select-view module="shangpinfenlei" :value="row.shangpinfenlei" select="id" show="fenleiming"></e-select-view> </template>
                    </el-table-column>
                    <el-table-column prop="shangpintupian" label="商品图片" width="100">
                        <template #default="{row}">
                            <div style="width: 100%; max-width: 120px">
                                <e-img :src="row.shangpintupian" type="list"></e-img>
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column prop="shangpinfujian" label="商品附件">
                        <template #default="{row}"> <e-file-list v-model="row.shangpinfujian"></e-file-list> </template>
                    </el-table-column>
                    <el-table-column sortable="custom" prop="jiage" label="价格" width="80">
                        <template #default="{row}"> {{ row.jiage }} </template>
                    </el-table-column>
                    <el-table-column sortable="custom" prop="xiaoliang" label="销量" width="80">
                        <template #default="{row}"> {{ row.xiaoliang }} </template>
                    </el-table-column>
                    <el-table-column prop="zhuangtai" label="状态" width="130">
                        <template #default="{row}"> {{ row.zhuangtai }} </template>
                    </el-table-column>
                    <el-table-column prop="faburen" label="发布人" width="80">
                        <template #default="{row}"> {{ row.faburen }} </template>
                    </el-table-column>
                    <el-table-column prop="addtime" label="发布时间">
                        <template #default="{row}"> {{ row.addtime.substring(0,19) }} </template>
                    </el-table-column>

                    <el-table-column width="80" label="上下架">
                        <template #default="{row}">

                            <template v-if="row.zhuangtai == '通过'">
                            <el-button size="small" :type="row.issh == '是' ? 'success' : 'danger'" @click="canShangpinxinxiCheckIssh(row)">
                                {{ row.issh == '是' ? '下架' : '上架' }}
                            </el-button>
                            </template>
                        </template>
                    </el-table-column>

                    <el-table-column label="操作" fixed="right" width="180">
                        <template #default="{row}">
                            <el-button-group>

                                <template v-if="row.zhuangtai == '待审核'">
                                    <el-button type="primary" :icon="Plus" size="small" @click="$router.push('/admin/shangpinshenhe/add?id='+row.id)">商品审核</el-button>
                                </template>

                                <el-tooltip effect="dark" content="详情" placement="top-start"
                                    ><el-button type="info" :icon="InfoFilled" size="small" @click="$router.push('/admin/shangpinxinxi/detail?id='+row.id)">详情</el-button>
                                </el-tooltip>
                                <el-tooltip effect="dark" content="编辑" placement="top-start"
                                    ><el-button type="success" :icon="Edit" size="small" @click="$router.push('/admin/shangpinxinxi/updt?id='+row.id)">编辑</el-button>
                                </el-tooltip>
                                <el-tooltip effect="dark" content="删除" placement="top-start"
                                    ><el-button type="danger" :icon="Delete" size="small" @click="deleteItems(row.id)">删除</el-button>
                                </el-tooltip>
                            </el-button-group>
                        </template>
                    </el-table-column>
                </el-table>
                <div class="e-pages" style="margin-top: 10px; text-align: center">
                    <el-pagination
                        @current-change="loadList"
                        :page-sizes="[12, 24, 36, 48,60]"
                        v-model:current-page="search.page"
                        v-model:page-size="search.pagesize"
                        @size-change="sizeChange"
                        layout="total, sizes, prev, pager, next"
                        :total="totalCount"
                    >
                    </el-pagination>
                </div>
            </el-card>
        </div>
    </div>
</template>

<script setup>
    import http from "@/utils/ajax/http";
    import DB from "@/utils/db";
    import router from "@/router";

    import { ref, reactive, watch, unref, onBeforeMount } from "vue";
    import { useRoute } from "vue-router";
    import { session } from "@/utils/utils";
    import { canShangpinxinxiSelect, useShangpinxinxiSelect, canShangpinxinxiDelete, canShangpinxinxiCheckIssh } from "@/module";
    import { extend } from "@/utils/extend";
    import { ElMessageBox, ElMessage } from "element-plus";
    import { Plus, InfoFilled, Edit, Delete } from "@element-plus/icons-vue";

    const route = useRoute();
    const search = reactive({
        shangpinmingcheng: "",
        shangpinfenlei: "",
        jiage_start: "",
        jiage_end: "",
        zhuangtai: "",
        addtime_start: "",
        addtime_end: "",
        page: 1, // 当前页
        pagesize: 12, // 每页行数
        orderby: "id", // 排序字段
        sort: "desc", // 排序类型
    });
    extend(search, route.query);
    // 链接参数变化时更新这些内容
    watch(
        () => route.query,
        () => {
            extend(search, route.query);
            loadList(1);
        },
        { deep: true }
    );

    // 总行数
    const totalCount = ref(0);
    // 列表数据
    const lists = ref([]);
    // 加载状态
    const loading = ref(false);

    // 排序操作
    const sortChange = (e) => {
        console.log(e);
        if (e.order == null) {
            search.orderby = "id";
            search.sort = "desc";
        } else {
            search.orderby = e.prop;
            search.sort = e.order == "ascending" ? "asc" : "desc";
        }
        loadList(1);
    };
    // 设置页数多少
    const sizeChange = (e) => {
        search.pagesize = e;
        loadList(1);
    };

    const deleteItems = (ids) => {
        return new Promise((resolve, reject) => {
            ElMessageBox.confirm("确定删除？")
                .then((res) => {
                    canShangpinxinxiDelete(ids).then((res) => {
                        loadList(search.page);
                        resolve(res.data);
                    });
                })
                .catch((err) => {
                    reject(err);
                });
        });
    };

    // 加载商品信息列表方法
    const loadList = (page) => {
        // 加载
        if (unref(loading)) return;
        loading.value = true;
        search.page = page;

        http.post("/api/shangpinxinxi/selectPages", search).then(
            (res) => {
                loading.value = false;
                if (res.code == 0) {
                    var data = res.data;
                    lists.value = data.lists.records;
                    totalCount.value = data.lists.total;
                }
            },
            (err) => {
                ElMessage.error(err.message);
            }
        );
    };

    onBeforeMount(() => {
        loadList(1);
    });
    const searchSubmit = () => {
        loadList(1);
    };
</script>

<style scoped lang="scss">
    .views-shangpinxinxi-list {
    }
</style>
